module.exports = function printInventory(inputs) {
    let allItems = Item.loadAllItems();
    let promotions = Item.loadPromotions()[0].barcodes ;
    let allInputs = allInputs(inputs);
    let allPromotions = allPromotions(allInputs,promotions) ;
    let arr = finalAnswer(allInputs,allItems,allPromotions);
    let menu = '***<没钱赚商店>购物清单***\n' +arr[0]+
        '----------------------\n' +
        '挥泪赠送商品：\n' +arr[1]+
        '----------------------\n' +
        `总计：${arr[2]}(元)\n` +
        `节省：${arr[3]}(元)\n` +
        '**********************';
        console.log(menu) ;
        return 'Hello world';
}
    

    function allInputs(inputs){
      let allInputs = {};
      for(let item of inputs){
        if(item.length !== 10){
            let arr = item.split('-');
            if(!allInputs[arr[0]]){
                allInputs[arr[0]] = arr[1] ;
            }else{
                allInputs[arr[0]] += arr[1] ;
            }
        }else{
            if(!allInputs[item]){
                allInputs[item] = 1 ;
            }else{
                  allInputs[item]++ ;
            }   
        }
      }
        return allInputs ;
    }
    
    function allPromotions(obj_in,promotions){
        let obj = {} ;
        for(let key in obj_in){
            if(promotions.indexOf(key) !== -1){
                obj[key] = parseInt(obj_in[key]/3) ;
            }
        }
        return obj ;
    }

    function finalAnswer(obj_in,allItems,allPromotions){
        let showPay = '' ;
        let showPro = '' ;
        let inputsArr = [] ;
        let pay = 0;  
        let cutPay = 0 ; 
        for(let key in obj_in){
          for(let item of allItems){
            if(item.barcode === key){
              if(allPromotions[key]){
                showPro += `名称：${item.name}，数量：${allPromotions[key]}${item.unit}\n` ;
                let one_price= item.price * (obj_in[key] - allPromotions[key]) ;
                pay += one_price ;
                cutPay += item.price * allPromotions[key] ;
                showPay +=  `名称：${item.name}，数量：${obj_in[key]}${item.unit}，单价：${item.price.toFixed(2)}(元)，小计：${one_price.toFixed(2)}(元)\n`
              }else{
                let one_price= item.price * (obj_in[key]) ;
                pay += one_price ;
                showPay +=  `名称：${item.name}，数量：${obj_in[key]}${item.unit}，单价：${item.price.toFixed(2)}(元)，小计：${one_price.toFixed(2)}(元)\n`
              }
            }
          }
        }
        inputsArr.push(showPay);
        inputsArr.push(showPro);
        inputsArr.push(pay.toFixed(2)) ;
        inputsArr.push(pro_pay.toFixed(2));
        return inputsArr;
    };


    const Item= require('./datbase.js');